#include "SceneManager.h"
#include <exception>

Ptr<Scene> SceneManager::currentScene;
std::list<Ptr<Scene> > SceneManager::sceneQueue;

SceneManager::SceneManager() {
}

void SceneManager::enqueue(Ptr<Scene> scene) {
	SceneManager::sceneQueue.push_back(scene);
}

void SceneManager::push(Ptr<Scene> scene) {
	SceneManager::sceneQueue.push_front(scene);
}

Ptr<Scene> SceneManager::pop() {
	Ptr<Scene> poppedScene = SceneManager::sceneQueue.front();
	SceneManager::sceneQueue.pop_front();
	return poppedScene;
}

void SceneManager::cleanUp() {
	/*
	for (Scene scene : SceneManager::sceneQueue) {
		scene.finish();
	}
	**/
}

void SceneManager::run() {
	if (SceneManager::currentScene.isNull()) {
		if (SceneManager::sceneQueue.empty()) {
			throw new std::exception;
		}
		SceneManager::currentScene = SceneManager::pop();
		SceneManager::currentScene->initialize();
	}
	while (!SceneManager::currentScene.isNull()) {
		SceneManager::currentScene->readInput();
		SceneManager::currentScene->update();
		if (!SceneManager::currentScene->canSkipFrame()) {
			SceneManager::currentScene->draw();
		}
		SceneManager::currentScene->sleep();
		SceneManager::currentScene = SceneManager::currentScene->getNextScene();
	}
}
